package org.mnswf.net {
	
	import flash.events.EventDispatcher;
	import org.mnswf.events.UDPDataEvent;
	import mdm.Network;
	
	/**
	 * @author Danny Patterson
	 */
	public class UDPSocket extends EventDispatcher {
		
		public function UDPSocket() {
			mdm.Network.UDP.Socket.onData = onPacketReceived;
			//mdm.Exception.enableHandler();
		}
		
		private function onPacketReceived(packet:Object):void {
			dispatchEvent(new UDPDataEvent(UDPDataEvent.DATA, new UDPPacket(packet.data)));
		}
		
		/**
		 * @param packet (UDPPacket)
		 * @param port (int)
		 */
		public function broadcast(port:int, packet:UDPPacket):void {
			mdm.Network.UDP.Socket.broadcast(port, packet.toString());
		}
		
		/**
		 * ipAddress (String)
		 * @param port (int)
		 * @param packet (UDPPacket)
		 */
		public function send(ipAddress:String, port:int, packet:UDPPacket):void {
			mdm.Network.UDP.Socket.send(ipAddress, port, packet.toString());
		}
		
		/**
		 * @param port (int)
		 */
		public function start(port:int):void {
			try {
				mdm.Network.UDP.Socket.startServer(port);
			}catch(error:Error) {}
		}
		
		/**
		 * 
		 */
		public function stop():void {
			mdm.Network.UDP.Socket.stopServer();
		}
		
	}
	
}